var totalPage;
var totalCnt;
var vehicleLineData = [];
var deleteflag ;

$(document).ready(function() {
	showLoading();
	$('title', parent.document).html($('title').text());
	setBaseUrl();
	$.ajax({
		type : 'post',
		url : 'lineName',
		dataType : 'json',
		success : function(data) {
			for (var i = 0; i < data.length; i++) {
				$("#lineID").append(
						"<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
				$("#saveLineID").append(
						"<option value='" + data[i].lineID + "'>" + data[i].lineName + "</option>");
			}
		}
	})
	$.ajax({
		type : 'post',
		url : 'carVIN',
		dataType : 'json',
		success : function(data) {
			for (var i = 0; i < data.length; i++) {
				$("#carVIN").append(
						"<option value='" + data[i].carVin + "'>" + data[i].carNo + "</option>");
				$("#saveCarVIN").append(
						"<option value='" + data[i].carVin + "'>" + data[i].carNo + "</option>");
			}
		}
	})
	setTimeout(hideLoading, 500);
})
function searchVehicleLine() {
	$("#page").val(1);
	$("#currentPage").val(1);
	search();
}
function search() {
	clearTable("carForLineTbody");
	$("#chkAll").attr("checked", false);
	seach=1;
	showLoading();
	$.ajax({
		type : 'post',
		data : $("#vehicleSearch").serialize(),
		url : 'searchVehicleLine',
		dataType : 'json',
		success : function(data) {
			setTimeout(hideLoading, 500);
			totalPage = data && data.totalPage || 0;
			totalCnt = data && data.totalCnt || 0;
			vehicleLineData = data && data.carForLineList || [];
			document.getElementById("total").innerText = '共' + totalCnt + '条 / 共' + totalPage + '页';
			// 当前页数
			var currentPage = $('#currentPage').val();
			// 每页显示的条数
			var rows = $('#rows').val();
			var length = vehicleLineData.length;
			if (length == 0) {
				showMessge("没有查询结果");
			} else {
				for(var i=0; i<length; i++){
					// 序号
					var rowNo = (currentPage - 1) * rows + i + 1;
					// 画面表格的body
					var tbody = $("#carForLineTbody");
					// 追加显示数据
					var $tr = $('<tr>').appendTo(tbody);
					$("<td>").html('<input name="chk" id="chk" type="checkbox">').appendTo($tr)
					$("<td>").html(rowNo).appendTo($tr);
					$("<td>").html(vehicleLineData[i].lineName).appendTo($tr);
					$("<td>").html(vehicleLineData[i].carNo).appendTo($tr);
				}
			}
		},
		error : function(data) {
			setTimeout(hideLoading, 500);
			if (data.status == "403" && data.statusText == "Forbidden") {
				showMessge("您没有操作权限!");
			} else {
				showMessge("查询失败!");
			}
		},
		timeout: function(data){
			setTimeout(hideLoading, 500);
			showMessge("操作超时！");
		}
	});
}
function save(){
	formReset("saveForm");
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '线路车辆信息添加';
	$("#saveCarVIN").attr("disabled",false);
    $("#save").modal("show");
    deleteflag = "save";
	saveTitle.append(strong);
}
function update(){
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count;
	var index = checkbox && checkbox.row_index;
	formReset("saveForm");
	$('#saveTitle').html('');
	var saveTitle = $('#saveTitle');
	var strong = document.createElement("strong");
	strong.innerHTML = '线路车辆信息修改';
		if (count == 1) {
			$('#save').modal('show');
			$("#saveLineID").val(vehicleLineData[index].lineName);
			$("#saveCarVIN").val(vehicleLineData[index].carNo).attr("disabled",true);
		} else if (count == 0) {
			showMessge("请选择要操作的数据!");
		} else {
			showMessge("抱歉,不能选择多行!");
		}
		deleteflag = "update";
	//初始化是否为空
	saveTitle.append(strong);
}
function confirm(){
	var saveLineID = $("#saveLineID").val();
	var saveCarVIN = $("#saveCarVIN").val();
			if (!saveLineID) {
				showMessge("线路不能为空！");
				return;
			}
			if (!saveCarVIN) {
				showMessge("车牌号不能为空！");
				return;
			}
		if(deleteflag == "save"){
			showLoading();
			$.ajax({
				type: "POST",
				url: 'saveVehicleLine',
		    	data: {
		    		LineID : saveLineID,
					CarVIN : saveCarVIN,
		    	},
		    	dataType : 'json',
				success: function(data){
					setTimeout(hideLoading, 500);
					$('#save').modal('hide');
					var result = data && data.result;
					if (result == "Success") {
						showMessge("添加成功");
					} else if (result == "NotFound") {
						showMessge("添加失败");
					} else if (result == "CarNoNoNull") {
						showMessge("车牌号已存在");
					} else {
						showMessge("添加异常");
					}
					search();
					formReset("saveForm");
				},
				error : function(data) {
					setTimeout(hideLoading, 500);
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge("您没有操作权限!");
					} else {
						showMessge("添加失败!");
					}
				},
				timeout: function(data){
					setTimeout(hideLoading, 500);
					showMessge("操作超时！");
				}
			});
		}else if(deleteflag == "update"){
			showLoading();
			$.ajax({
				type : "post",
				url : "updateVehicleLine",
				dataType : "json",
				data : {
					LineID : saveLineID,
					CarVIN : saveCarVIN,
				},
				success : function(data) {
					setTimeout(hideLoading, 500);
					$('#save').modal('hide');
					var result = data && data.result;
					if (result == "Success") {
						showMessge("修改成功");
					} else if (result == "NotFound") {
						showMessge("修改失败");
					} else if (result == "CarNoNoNull") {
						showMessge("车牌号已存在");
					} else {
						showMessge("修改异常");
					}
					search();
				},
				error : function(data) {
					setTimeout(hideLoading, 500);
					if (data.status == "403" && data.statusText == "Forbidden") {
						showMessge("您没有操作权限!");
					} else {
						showMessge("修改失败!");
					}
				},
				timeout: function(data){
					setTimeout(hideLoading, 500);
					showMessge("操作超时！");
				}
			});
		}
	}
// 删除
function sure(){
	var checkbox = getCheckbox("chk");
	var count = checkbox && checkbox.count || 0;
	var index = undefined;
	var ID = undefined;
	var IDList = [];
	for (var i=0; i<count; i++) {
		index = checkbox && checkbox.row_index[i] || 0;
		ID = vehicleLineData && vehicleLineData[index] && vehicleLineData[index].id;
		IDList.push(ID);
	}
	if(saveFlag=="delete"){
		showLoading();
		$.ajax({
			type : "post",
			url : "deleteVehicleLine",
			dataType : "json",
			data : {
				IDList : IDList.toString()
			},
			success : function(data) {
				setTimeout(hideLoading, 500);
				var result = data && data.result;
				if (result == "Success") {
					showMessge("删除成功");
				} else if (result == "NotFound") {
					showMessge("删除数据不存在");
				} else {
					showMessge("删除失败");
				}
				search();
			},
			error : function(data) {
				setTimeout(hideLoading, 500);
				if (data.status == "403" && data.statusText == "Forbidden") {
					showMessge("没有访问权限!");
				} else {
					showMessge("操作失败!");
				}
			},
			timeout: function(data){
				setTimeout(hideLoading, 500);
				showMessge("操作超时！");
			}
		})
	}
}
